package net.hzlj.platform.enterprise.activity;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;

import net.hzlj.platform.enterprise.BootApp;
import net.hzlj.platform.enterprise.common.Constant;
import net.hzlj.platform.enterprise.receiver.DeviceReceiver;
import net.hzlj.platform.enterprise.utils.AppPreferencesUtils;
import net.hzlj.platform.enterprise.utils.PreferencesUtils;
import net.hzlj.rl.zhangjiajie.R;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class DeviceAuthorizeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_device_authorize);
    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean flag = AppPreferencesUtils.getBoolean(BootApp.getAppContext(), PreferencesUtils.KEY_ALLOW_UNINSTALLATION, Constant.Socket.DEFAULT_ALLOW_UNINSTALL);
        if (flag){
            finish();
        }else{
            DevicePolicyManager mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
            ComponentName mDeviceComponentName = new ComponentName(this.getApplicationContext(),
                    DeviceReceiver.class);
            if (mDPM.isAdminActive(mDeviceComponentName) == false) {
                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceComponentName);
                startActivity(intent);
            } else {
                finish();
            }
        }
    }
}
